import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../../common/system_config.dart';
import '../../pages/alarm_view/alarm_view.dart';
import '../../pages/home_view/home_view.dart';
import '../../pages/personalcenter_view/personalcenter_view.dart';
import '../../utils/tools/screenutil_tool.dart';
import '../../utils/widget/toast_widget.dart';

class LayoutView extends StatefulWidget {
  @override
  LayoutViewState createState() => new LayoutViewState();
}

class LayoutViewState extends State<LayoutView> {
  // 默认显示的组件
  int _currentIndex = 0;

  // 存储操作时间差（用于判断点击系统返回按钮是否退出系统）
  DateTime _lastQuitTime;

  //底部导航栏显示的内容
  final List<BottomNavigationBarItem> bottomNavItems = [
    BottomNavigationBarItem(
      icon: Icon(Icons.donut_small),
      title: Text("信息总览", style: TextStyle(fontSize: ST.setsp(26))),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.info),
      title: Text("告警信息", style: TextStyle(fontSize: ST.setsp(26))),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.account_circle),
      title: Text("个人中心", style: TextStyle(fontSize: ST.setsp(26))),
    )
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WillPopScope(
        onWillPop: () async {
          if (_lastQuitTime == null ||
              DateTime.now().difference(_lastQuitTime).inSeconds > 1) {
            ToastWidget.toast("再按一次退出系统",
                position: EasyLoadingToastPosition.bottom);
            _lastQuitTime = DateTime.now();
            return false;
          }
          return true;
        },
        child: renderView(_currentIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        fixedColor: SystemConfig.themeColor,
        items: bottomNavItems,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }

  /// 渲染view
  /// * [i] int|必填|显示内容对应的下标
  Widget renderView(int i) {
    List<Widget> lw = [
      HomeView(callBack: (Map e) {
        setState(() {
          _currentIndex = e["currentIndex"];
        });
      }),
      AlarmView(),
      PersonalCenterView()
    ];
    return lw[i];
  }
}
